home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / dev / lang / sofa.lha / sofa / smalleiffel / lib_std / general.e < prev    next >
Text File  |  2000-03-25  |  20KB  |  670 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  10. --                       http://SmallEiffel.loria.fr
  11. --
  12. class GENERAL
  13.    --
  14.    -- Platform-independent universal properties.
  15.    -- This class is an ancestor to all developer-written classes.
  16.    --
  17.  
  18. feature -- Access :
  19.    
  20.    generating_type: STRING is
  21.          -- Name of current object's generating type (type of 
  22.          -- which it is a direct instance).
  23.       external "SmallEiffel"
  24.       end;
  25.   
  26.    generator: STRING is
  27.          -- Name of current object's generating class (base class
  28.          -- of the type of which it is a direct instance).
  29.       external "SmallEiffel"
  30.       end;
  31.    
  32.    stripped(other: GENERAL): like other is
  33.       -- Newly created object with fields copied from current object,
  34.       -- but limited to attributes of type of `other'.
  35.       require
  36.          conformance: conforms_to(other);
  37.       do
  38.          not_yet_implemented;
  39.       ensure
  40.          stripped_to_other: Result.same_type(other);
  41.       end;
  42.    
  43. feature -- Status report :
  44.    
  45.    frozen conforms_to(other: GENERAL): BOOLEAN is
  46.          -- Is dynamic type of `Current' a descendant of dynamic type of
  47.          -- `other' ?
  48.          -- 
  49.          -- Note: because of automatic conversion from expanded to reference
  50.          -- type when passing argument `other', do not expect a correct  
  51.          -- behavior with  expanded types.
  52.       require 
  53.          not is_expanded_type;
  54.          other_not_void: other /= Void
  55.       do
  56.          Result := other.se_assigned_from(Current);
  57.       end;
  58.  
  59.    frozen same_type(other: GENERAL): BOOLEAN is
  60.          -- Is type of current object identical to type of `other' ?
  61.       require
  62.          not is_expanded_type;
  63.          other_not_void: other /= Void
  64.       do
  65.          if conforms_to(other) then
  66.             Result := other.conforms_to(Current);
  67.          end;
  68.       ensure
  69.          definition: Result = (conforms_to(other) 
  70.                                and 
  71.                                other.conforms_to(Current));
  72.       end;
  73.    
  74. feature -- Comparison :
  75.    
  76.    frozen equal(some: ANY; other: like some): BOOLEAN is
  77.          -- Are `some' and `other' both Void or attached to
  78.          -- objects considered equal ?
  79.       do
  80.          if some = other then
  81.             Result := true;
  82.          elseif some = Void then
  83.          elseif other = Void then
  84.          else
  85.             Result := some.is_equal(other);
  86.          end;
  87.       ensure
  88.          definition: Result = (some = Void and other = Void) or else
  89.                      ((some /= Void and other /= Void) and then
  90.                       some.is_equal(other));
  91.       end;
  92.  
  93.    is_equal(other: like Current): BOOLEAN is
  94.          -- Is `other' attached to an object considered equal to 
  95.          -- current object ?
  96.       require
  97.          other_not_void: other /= Void
  98.       external "SmallEiffel"
  99.       ensure
  100.          consistent: standard_is_equal(other) implies Result;
  101.          symmetric: Result implies other.is_equal(Current);
  102.       end;
  103.    
  104.    frozen standard_equal(some: ANY; other: like some): BOOLEAN is
  105.          -- Are `some' and `other' both Void or attached to
  106.          -- field-by-field objects of the same type ?
  107.          -- Always use the default object comparison criterion.
  108.       do
  109.          if some = other then
  110.             Result := true;
  111.          elseif some = Void then
  112.          elseif other = Void then
  113.          elseif some.same_type(other) then
  114.             Result := some.standard_is_equal(other);
  115.          end;
  116.       ensure
  117.          definition: Result = (some = Void and other = Void) or else
  118.                      ((some /= Void and other /= Void) and then
  119.                       some.standard_is_equal(other));
  120.       end;
  121.  
  122.    frozen standard_is_equal(other: like Current): BOOLEAN is
  123.          -- Are Current and `other' field-by-field identical?
  124.       require
  125.          other /= Void
  126.       external "SmallEiffel"
  127.       ensure
  128.          symmetric: Result implies other.standard_is_equal(Current);
  129.       end;
  130.    
  131. feature -- Deep Comparison :
  132.  
  133.    frozen deep_equal(some: ANY; other: like some): BOOLEAN is
  134.          -- Are `some' and `other' either both Void or attached to 
  135.          -- recursively isomorphic object structures ?
  136.       do
  137.          if some = other then
  138.             Result := true;
  139.          elseif some = Void then
  140.          elseif other = Void then
  141.          elseif standard_equal(some,other) then
  142.             Result := true;
  143.          else
  144.             Result := some.is_deep_equal(other);
  145.          end;
  146.       ensure
  147.          shallow_implies_deep: standard_equal(some,other) implies Result
  148.       end;
  149.    
  150.    is_deep_equal(other: like Current): BOOLEAN is
  151.          -- Is `Current' recursively isomorph with `other' ?
  152.       require
  153.          other_not_void: other /= Void
  154.       external "SmallEiffel"
  155.       end;
  156.    
  157. feature -- Duplication :
  158.    
  159.    frozen clone(other: ANY): like other is
  160.          -- When argument `other' is Void, return Void
  161.          -- otherwise return `other.twin'.
  162.       do
  163.          if other /= Void then
  164.             Result := other.twin;
  165.          end;
  166.       ensure
  167.          equal: equal(Result,other);
  168.       end;
  169.  
  170.    frozen twin: like Current is
  171.          -- Return a new object with the dynamic type of Current.
  172.          -- Before being returned, the new object is initialized using
  173.          -- feature `copy' (Current is passed as the argument).
  174.          -- Thus, when feature `copy' of GENERAL is not redefined, 
  175.          -- `twin' has exactly the same behaviour as `standard_twin'.
  176.       external "SmallEiffel"
  177.       ensure
  178.          equal: Result.is_equal(Current);
  179.       end;
  180.  
  181.    copy(other: like Current) is
  182.          -- Update current object using fields of object attached
  183.          -- to `other', so as to yield equal objects.
  184.       require
  185.          other_not_void: other /= Void
  186.       external "SmallEiffel"
  187.       ensure
  188.          is_equal: is_equal(other)
  189.       end;
  190.    
  191.    frozen standard_clone(other: ANY): like other is
  192.          -- Void if `other' is Void; otherwise new object 
  193.          -- field-by-field identical to `other'. 
  194.          -- Always use the default copying semantics.
  195.       do
  196.          if other /= Void then
  197.             Result := other.standard_twin;
  198.          end;
  199.       ensure
  200.          equal: standard_equal(Result,other);
  201.       end;
  202.  
  203.    frozen standard_twin: like Current is
  204.          -- Return a new object with the dynamic type of Current.
  205.          -- Before being returned, the new object is initialized using
  206.          -- feature `standard_copy' (Current is passed as the argument).
  207.       external "SmallEiffel"
  208.       end;
  209.       
  210.    frozen standard_copy(other: like Current) is
  211.          -- Copy every field of `other' onto corresponding field of 
  212.          -- current object.
  213.       require
  214.          other_not_void: other /= Void
  215.       external "SmallEiffel"
  216.       ensure
  217.          standard_is_equal(other);
  218.       end;
  219.    
  220. feature -- Deep Duplication :
  221.  
  222.    frozen deep_clone(other: ANY): like other is
  223.          -- When argument `other' is Void, return Void
  224.          -- otherwise return `other.deep_twin'.
  225.       do
  226.      if other /= Void then
  227.         Result := other.deep_twin;
  228.      end;
  229.       ensure
  230.      deep_equal(other,Result)
  231.       end;
  232.    
  233.    deep_twin: like Current is
  234.          -- Return a new object with the dynamic type of Current.
  235.          -- The new object structure is recursively duplicated from the one 
  236.          -- attached to `Current'.
  237.       external "SmallEiffel"
  238.       end;
  239.  
  240. feature -- Basic operations :
  241.    
  242.    frozen default: like Current is
  243.          -- Default value of current type.
  244.       do
  245.       end;
  246.    
  247.    frozen default_pointer: POINTER is
  248.          -- Default value of type POINTER (avoid the need to
  249.          -- write p.default for some `p' of type POINTER).
  250.       do
  251.       ensure
  252.          Result = Result.default;
  253.       end;
  254.    
  255.    default_rescue is
  256.          -- Handle exception if no Rescue clause.
  257.          -- (Default: do nothing.)   
  258.       do
  259.       end;
  260.    
  261.    frozen do_nothing is
  262.          -- Execute a null action.
  263.       do
  264.       end;
  265.    
  266. feature -- Input and Output :
  267.    
  268.    io: STD_INPUT_OUTPUT is
  269.          -- Handle to standard file setup.
  270.          -- To use the standard input/output file.
  271.          -- Has type STD_FILES in ELKS 95.
  272.       once
  273.          !!Result.make;
  274.       ensure
  275.          Result /= Void;
  276.       end; 
  277.    
  278.    std_input: STD_INPUT is
  279.          -- To use the standard input file.
  280.       once
  281.          !!Result.make;
  282.       end; 
  283.    
  284.    std_output: STD_OUTPUT is
  285.          -- To use the standard output file.
  286.       once
  287.          !!Result.make;
  288.       end; 
  289.    
  290.    std_error: STD_ERROR is
  291.          -- To use the standard error file.
  292.       once
  293.          !!Result.make;
  294.       end; 
  295.    
  296. feature -- Object Printing :
  297.  
  298.    frozen print(some: GENERAL) is
  299.          -- Write terse external representation of `some' on
  300.          -- `standard_output'.
  301.          -- To customize printing, one may redefine 
  302.          -- `fill_tagged_out_memory' or `out_in_tagged_out_memory' (see 
  303.          -- for example how it works in class COLLECTION).
  304.       -- Not frozen in ELKS 95.
  305.       do
  306.          if some = Void then
  307.             std_output.put_string("Void");
  308.          else
  309.             some.print_on(std_output);
  310.          end;
  311.       end;
  312.    
  313.    print_on(file: OUTPUT_STREAM) is
  314.          -- Default printing of current object on a `file'.
  315.          -- One may redefine `fill_tagged_out_memory' or 
  316.          -- `out_in_tagged_out_memory' to adapt the behavior of 
  317.          -- `print_on'.
  318.          --
  319.       do
  320.          tagged_out_memory.clear;
  321.          out_in_tagged_out_memory;
  322.          file.put_string(tagged_out_memory);
  323.       end;
  324.  
  325.    frozen tagged_out: STRING is
  326.          -- New string containing printable representation of current 
  327.          -- object, each field preceded by its attribute name, a 
  328.          -- colon and a space.
  329.       do
  330.          tagged_out_memory.clear;
  331.          fill_tagged_out_memory;
  332.          Result := tagged_out_memory.twin;
  333.       end;
  334.    
  335.    out: STRING is
  336.          -- Create a new string containing terse printable 
  337.          -- representation of current object.
  338.       do
  339.          tagged_out_memory.clear;
  340.          out_in_tagged_out_memory;
  341.          Result := tagged_out_memory.twin;
  342.       end;
  343.    
  344.    out_in_tagged_out_memory is
  345.          -- Append terse printable represention of current object
  346.          -- in `tagged_out_memory'.
  347.       do
  348.          tagged_out_memory.append(generating_type);
  349.          if not is_expanded_type then
  350.             tagged_out_memory.extend('#');
  351.             Current.to_pointer.append_in(tagged_out_memory);
  352.          end;
  353.          tagged_out_memory.extend('[');
  354.          fill_tagged_out_memory;
  355.          tagged_out_memory.extend(']');
  356.       end;
  357.    
  358.    frozen tagged_out_memory: STRING is
  359.       once
  360.          !!Result.make(1024);
  361.       end;
  362.    
  363.    fill_tagged_out_memory is
  364.          -- Append a viewable information in `tagged_out_memory' in 
  365.          -- order to affect the behavior of `out', `tagged_out', etc.
  366.       do
  367.          -- Should be an external "SmallEiffel" to provide a default 
  368.          -- view of Current contents (not yet implemented).
  369.       end;
  370.    
  371. feature -- Basic named file handling :
  372.  
  373.    frozen file_tools: FILE_TOOLS is
  374.       once
  375.       end;
  376.    
  377.    file_exists(path: STRING): BOOLEAN is
  378.          -- True if `path' is an existing readable file.
  379.       require
  380.          not path.is_empty
  381.       do
  382.          Result := file_tools.is_readable(path);
  383.       end;
  384.    
  385.    remove_file(path: STRING) is
  386.       require
  387.          path /= Void;
  388.       do
  389.          file_tools.delete(path);
  390.       end;
  391.    
  392.    rename_file(old_path, new_path: STRING) is
  393.       require
  394.          old_path /= Void;
  395.          new_path /= Void
  396.       do
  397.          file_tools.rename_to(old_path,new_path);
  398.       end;
  399.  
  400. feature -- Access to command-line arguments :
  401.    
  402.    argument_count: INTEGER is
  403.          -- Number of arguments given to command that started
  404.          -- system execution (command name does not count).
  405.       do
  406.          Result := command_arguments.upper;
  407.       ensure
  408.          Result >= 0;
  409.       end;
  410.    
  411.    argument(i: INTEGER): STRING is
  412.          -- `i' th argument of command that started system execution 
  413.          -- Gives the command name if `i' is 0.
  414.       require
  415.          i >= 0;
  416.          i <= argument_count;
  417.       do
  418.          Result := command_arguments.item(i);
  419.       ensure
  420.          Result /= Void
  421.       end;
  422.  
  423.    frozen command_arguments: FIXED_ARRAY[STRING] is
  424.          -- Give acces to arguments command line including the
  425.          -- command name at index 0.
  426.       local
  427.          i: INTEGER;
  428.          arg: STRING;
  429.       once
  430.          from
  431.             i := se_argc;
  432.             !!Result.make(i);
  433.          until
  434.             i = 0
  435.          loop
  436.             i := i - 1;
  437.             arg := se_argv(i);
  438.             Result.put(arg,i);
  439.          end;
  440.       ensure
  441.          not Result.is_empty
  442.       end;
  443.    
  444.    get_environment_variable(name: STRING): STRING is
  445.          -- To get the value of a system environment variable
  446.          -- (like "PATH" on Unix for example).
  447.          -- Gives Void when system variable `name' is undefined.
  448.       require
  449.          name /= Void
  450.       local
  451.          p: POINTER;
  452.       do
  453.          p := name.to_external;
  454.          Result := se_getenv(p);
  455.       end;
  456.    
  457. feature -- System calls and crashs :
  458.    
  459.    frozen crash is
  460.          -- Print Run Time Stack and then exit with `exit_failure_code'.
  461.       do
  462.          print_run_time_stack;
  463.          die_with_code(exit_failure_code);
  464.       end;
  465.  
  466.    frozen trace_switch(flag: BOOLEAN) is
  467.          -- May be used in combination with option "-trace" of
  468.          -- command `compile_to_c' (see compile_to_c.hlp for
  469.          -- details).
  470.       external "SmallEiffel"
  471.       end;
  472.  
  473.    system(system_command_line: STRING) is
  474.          -- To execute a `system_command_line' as for example, "ls -l" on UNIX.
  475.       local
  476.          p: POINTER;
  477.       do
  478.          p := system_command_line.to_external;
  479.          se_system(p);
  480.       end;
  481.    
  482.    frozen die_with_code(code:INTEGER) is
  483.          -- Terminate execution with exit status code `code'.
  484.          -- Do not print any message.
  485.          -- Note: you can use predefined `exit_success_code' or
  486.          -- `exit_failure_code' as well as another code you need.
  487.       external "SmallEiffel"
  488.       end;
  489.    
  490.    exit_success_code: INTEGER is 0;
  491.    
  492.    exit_failure_code: INTEGER is 1;
  493.    
  494. feature -- Maths constants :
  495.  
  496.    Pi    : DOUBLE is  3.1415926535897932384626; 
  497.        
  498.    Evalue: DOUBLE is  2.7182818284590452353602;
  499.      
  500.    Deg   : DOUBLE is 57.2957795130823208767981; -- Degrees/Radian
  501.      
  502.    Phi   : DOUBLE is  1.6180339887498948482045; -- Golden Ratio
  503.  
  504. feature -- Character names :
  505.  
  506.    Ctrl_a: CHARACTER is '%/1/';
  507.    Ctrl_b: CHARACTER is '%/2/';
  508.    Ctrl_c: CHARACTER is '%/3/';
  509.    Ctrl_d: CHARACTER is '%/4/';
  510.    Ctrl_e: CHARACTER is '%/5/';
  511.    Ctrl_f: CHARACTER is '%/6/';
  512.    Ctrl_g: CHARACTER is '%/7/';
  513.    Ch_bs : CHARACTER is '%/8/';
  514.    Ch_tab: CHARACTER is '%/9/';
  515.    Ctrl_j: CHARACTER is '%/10/';
  516.    Ctrl_k: CHARACTER is '%/11/';
  517.    Ctrl_l: CHARACTER is '%/12/';
  518.    Ctrl_m: CHARACTER is '%/13/';
  519.    Ctrl_n: CHARACTER is '%/14/';
  520.    Ctrl_o: CHARACTER is '%/15/';
  521.    Ctrl_p: CHARACTER is '%/16/';
  522.    Ctrl_q: CHARACTER is '%/17/';
  523.    Ctrl_r: CHARACTER is '%/18/';
  524.    Ctrl_s: CHARACTER is '%/19/';
  525.    Ctrl_t: CHARACTER is '%/20/';
  526.    Ctrl_u: CHARACTER is '%/21/';
  527.    Ctrl_v: CHARACTER is '%/22/';
  528.    Ctrl_w: CHARACTER is '%/23/';
  529.    Ctrl_x: CHARACTER is '%/24/';
  530.    Ctrl_y: CHARACTER is '%/25/';
  531.    Ctrl_z: CHARACTER is '%/26/';
  532.    Ch_del: CHARACTER is '%/63/';
  533.  
  534. feature -- Should not exist :
  535.    
  536.    not_yet_implemented is
  537.       do
  538.          std_error.put_string(
  539.           "Sorry, Some Feature is Not Yet Implemented.%N%
  540.            %Please, if you can write it by yourself and if you send me%N%
  541.            %the corresponding tested Eiffel code, I may put it in the%N% 
  542.            %standard library!%N%
  543.            %Many Thanks in advance.%N%  
  544.            %D.Colnet e-mail: colnet@loria.fr%N");
  545.          crash;
  546.        end;
  547.  
  548. feature -- For ELS Compatibility :
  549.  
  550.    id_object(id: INTEGER): ANY is
  551.          -- Object for which `object_id' has returned `id';
  552.          -- Void if none.
  553.       require
  554.          id /= 0;
  555.       do
  556.          Result := object_id_memory.item(id);
  557.       end;
  558.    
  559.    object_id: INTEGER is
  560.          -- Value identifying current reference object.
  561.       require
  562.          not is_expanded_type
  563.       do
  564.          Result := object_id_memory.fast_index_of(Current);
  565.          if Result > object_id_memory.upper then
  566.             object_id_memory.add_last(Current);
  567.          end;
  568.       end;
  569.    
  570. feature -- The Guru section :
  571.    
  572.    to_pointer: POINTER is
  573.          -- Explicit conversion of a reference into POINTER type.
  574.       require
  575.          not is_expanded_type
  576.       external "SmallEiffel"
  577.       end;
  578.  
  579.    frozen is_expanded_type: BOOLEAN is
  580.          -- Target is not evaluated (Statically computed).
  581.          -- Result is true if target static type is an expanded type.
  582.          -- Useful for formal generic type.
  583.       external "SmallEiffel"
  584.       end;
  585.    
  586.    frozen is_basic_expanded_type: BOOLEAN is
  587.          -- Target is not evaluated (Statically computed).
  588.          -- Result is true if target static type is one of the 
  589.          -- following types: BOOLEAN, CHARACTER, INTEGER, REAL,
  590.          -- DOUBLE or POINTER.
  591.       external "SmallEiffel"
  592.       ensure
  593.          Result implies is_expanded_type
  594.       end;
  595.    
  596.    frozen object_size: INTEGER is
  597.          -- Gives the size of the current object at first level 
  598.          -- only (pointed-to sub-object are not concerned).  
  599.          -- The result is given in number of CHARACTER.
  600.       external "SmallEiffel"
  601.       end;
  602.  
  603. feature {NONE} -- The Guru section :
  604.    
  605.    c_inline_h(c_code: STRING) is
  606.          -- Target must be Current and `c_code' must be a manifest
  607.          -- string. Write `c_code' in the heading C file.
  608.       external "SmallEiffel"
  609.       end;
  610.  
  611.    c_inline_c(c_code: STRING) is
  612.          -- Target must be Current and `c_code' must be a manifest
  613.          -- string. Write `c_code' in the stream at current position.
  614.       external "SmallEiffel"
  615.       end;
  616.  
  617. feature {NONE} -- Implementation of GENERAL (do not use directly) :
  618.       
  619.    object_id_memory: ARRAY[ANY] is
  620.          -- For a portable implementation of `id_object'/`object_id'.
  621.          -- Note: I think that the pair `id_object'/`object_id' is
  622.          -- a stupid one. It should be removed.
  623.       once
  624.          !!Result.with_capacity(256,1);
  625.       end;
  626.  
  627.    print_run_time_stack is
  628.          -- Prints the run time stack.
  629.          -- The result depends both on compilation mode and 
  630.          -- target langage used (C or Java byte code).
  631.          -- Usually, in mode -boost, no information is printed.
  632.       external "SmallEiffel"
  633.       end;
  634.  
  635.    se_argc: INTEGER is
  636.          -- To implement `command_arguments'
  637.       external "SmallEiffel"
  638.       end;
  639.  
  640.    se_argv(i: INTEGER): STRING is
  641.          -- To implement `command_arguments'
  642.       external "SmallEiffel"
  643.       end;
  644.  
  645.    se_getenv(environment_variable: POINTER): STRING is
  646.          -- To implement `get_environment_variable'.
  647.       external "SmallEiffel"
  648.       end;
  649.       
  650.    se_system(system_command_line: POINTER) is
  651.       external "SmallEiffel"
  652.       end;
  653.  
  654. feature -- Implementation of GENERAL (do not use directly) :
  655.       
  656.    frozen se_assigned_from(other: GENERAL): BOOLEAN is
  657.          -- To implement `conforms_to' (must only be called inside
  658.          -- `conforms_to' because of VJRV rule).
  659.       require
  660.          not is_expanded_type
  661.       local
  662.          x: like Current;
  663.       do
  664.          x ?= other;
  665.          Result := x /= Void;
  666.       end;
  667.  
  668. end -- GENERAL
  669.  
  670.